package ks.server.controllers.requests;

import java.util.Collection;
import java.util.Hashtable;

import org.w3c.dom.Document;

import ks.framework.common.Message;
import ks.framework.communicator.Communicator;
import ks.managers.user.User;

public class UsersRequest {
	private Communicator com;
	
	public UsersRequest(Communicator c) {
		this.com = c;
	}
	
	public boolean send(Collection<User> users, Hashtable<String, Boolean> online) {
		StringBuilder sb = new StringBuilder("<request version=\"1.0\" id=\"12345\">");
		sb.append("<users>");
		for (User u : users)
			sb.append("<allplayer name=\"" + u.getName() + "\" rating=\"" + u.getRating() + "\" icon=\"" + u.getIconName() + "\" online=\"" + online.get(u.getName()) + "\"/>");
		sb.append("</users>");
		sb.append("</request>");
		
		Document d = Message.construct(sb.toString());
		Message r = new Message(d);
		r.setBroadcast();
		return com.distribute(r);
	}
	
	public boolean send(Collection<User> users, Hashtable<String, Boolean> online, Collection<String> recipients) {
		StringBuilder sb = new StringBuilder("<request version=\"1.0\" id=\"12345\">");
		sb.append("<users>");
		for (User u : users)
			sb.append("<allplayer name=\"" + u.getName() + "\" rating=\"" + u.getRating() + "\" icon=\"" + u.getIconName() + "\" online=\"" + online.get(u.getName()) + "\"/>");
		sb.append("</users>");
		sb.append("</request>");
		
		Document d = Message.construct(sb.toString());
		Message r = new Message(d);
		return com.distribute(recipients.iterator(), r);
	}
}